package my.maven.project.spiderproc.spiders;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import reactor.core.support.Exceptions;

public class CibaSpider {

	private static String site = "http://www.iciba.com/";

	public static Map<String, String> ReuqestHttp(String words) throws IOException {
		Map<String, String> wordsMap =null; 
		String url = String.format("%s%s", site, words); 
		Document doc = Jsoup.connect(url).get();

		Element container = doc.getElementsByClass("container").first();

		if (container != null) {
			wordsMap = new HashMap<>(); 
			Element container_left = container.select(".container-left").first();
			Element keywords = container_left.select("h1.keyword").first();
			
			if (keywords != null) {
				wordsMap.put("words", keywords.text());
				String pronunce = "";
				Elements pronuceEl = container_left.select(".base-top-voice .base-speak span");
				for (Element item : pronuceEl) {
					Element i = item.selectFirst("i");
					if (i != null) {
						pronunce += item.selectFirst("span").text() + " " + i.attr("ms-on-mouseover");
					}
				}
				wordsMap.put("pronunce", pronunce.toString());
				StringBuilder means = new StringBuilder();
				Elements meansEl = container_left.select("ul li.clearfix");
				for (Element item : meansEl) {
					means.append(item.selectFirst("span").text() + " ");

					Elements meantp = item.select("p span");
					for (Element span : meantp) {
						means.append(span.text() + " ");
					}
				}
				wordsMap.put("means", means.toString());
				// change clearfix
				StringBuilder changes = new StringBuilder();
				Element changesEl = container_left.select("li.change.clearfix").first();
				if (changesEl != null) {
					changes.append(changesEl.selectFirst("h1").text() + " ");
					changes.append(changesEl.selectFirst("p span").text() + " ");
					changes.append(changesEl.selectFirst("p span a").text() + " ");
				}
				wordsMap.put("changes", changes.toString());
			}

		}

//			 Document containerDoc = Jsoup.parse(container.toString()); 
		// Elements clearfix = moduleDoc.getElementsByClass("clearfix"); //DOM的形式
//			Elements clearfix = container.select(".clearfix"); // 选择器的形式 
//			for (Element clearfixli : clearfix) {
//				Document clearfixliDoc = Jsoup.parse(clearfixli.toString());
//				Elements kind = clearfixliDoc.select(".board-tag"); // 选择器的形式
//				Elements title = clearfixliDoc.select(".tit-post");
//				Elements author = clearfixliDoc.select("span a");
//
//				System.out.println("类别" + kind.text()); // 分类
//				System.out.println("标题" + title.text()); // 标题
//				System.out.println("作者" + author.text()); // 作者
//				System.out.println("详情链接" + title.attr("href")); // 标题下的链接
//
//				System.out.println("=====================");
//
//			}
		// String title = clearfixli.getElementsByTag("a").text();
		// System.out.println(clearfix);

		return wordsMap;

	}
}
